import org.apache.zookeeper.*;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;

/**
 * @author create on 2021/7/2
 */
public class ZookeeperCreateTest implements Watcher {

    public static void main(String[] args) throws Exception {
        ZooKeeper zookeeper1 = new ZooKeeper("127.0.0.1:2181", 5000, new ZookeeperCreateTest());
        ZooKeeper zookeeper2 = new ZooKeeper("127.0.0.1:2181", 5000, new ZookeeperCreateTest());
        System.out.println(zookeeper1.getState());
        Thread.sleep(2000);
        System.out.println(zookeeper1.getState());

        System.out.println("*****************");

//        zookeeper1.create("/nodeSequenceTest", "1233".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
//        zookeeper2.create("/nodeSequence", "1233".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
//        byte[] data = zookeeper1.getData("/nodeSequence", new ZookeeperCreateTest(), new Stat());
//        System.out.println(new String(data));
//
//        byte[] data2 = zookeeper2.getData("/nodeSequence", new ZookeeperCreateTest(), new Stat());
//        System.out.println(new String(data2));





        /*long sessionId = zookeeper1.getSessionId();
        byte[] sessionPasswd = zookeeper1.getSessionPasswd();
        ZooKeeper zookeeper2 = new ZooKeeper("127.0.0.1:2181", 5000, new ZookeeperCreateTest(), sessionId, sessionPasswd);
        System.out.println(zookeeper2.getState());
        Thread.sleep(2000);
        System.out.println(zookeeper2.getState());


        System.out.println("*** 创建节点 *** ");
//        zookeeper1.create("/node", "1233".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

        System.out.println("*****************");
        System.out.println("*** 创建临时顺序节点并且回调 *** ");

        zookeeper1.create("/node2", "1234567".getBytes(),
                Collections.singletonList(new ACL(ZooDefs.Perms.CREATE, new Id("ip", "127.0.0.1"))),
                        CreateMode.EPHEMERAL_SEQUENTIAL,
                        new AsyncCallback.StringCallback() {
                            public void processResult(int rc, String path, Object ctx, String name) {
                                System.out.println("rc:" + rc);
                                System.out.println("path:" + path);
                                System.out.println("ctx:" + ctx);
                                System.out.println("name:" + name);
                            }
                        }, "传递给服务端，然后异步回调传回");

        Thread.sleep(5000);


        System.out.println("*****************");
        System.out.println("*** 创建持久节点并且回调 *** ");

        zookeeper1.create("/node3", "1234567".getBytes(),
                Collections.singletonList(new ACL(ZooDefs.Perms.ALL, new Id("digest", "abc123"))),
                CreateMode.PERSISTENT,
                new AsyncCallback.StringCallback() {
                    public void processResult(int rc, String path, Object ctx, String name) {
                        System.out.println("rc:" + rc);
                        System.out.println("path:" + path);
                        System.out.println("ctx:" + ctx);
                        System.out.println("name:" + name);
                    }
                }, "传递给服务端，然后异步回调传回");

        Thread.sleep(5000);*/


//        System.out.println("*****************");
//        System.out.println("*** 同步获取数据 *** ");
//        byte[] data = zookeeper1.getData("/node", new ZookeeperCreateTest(), new Stat());
//        System.out.println(new String(data));
//
//
//        System.out.println("*****************");
//        System.out.println("*** 异步获取数据 并且回调 *** ");
//        zookeeper1.getData("/node", false, new AsyncCallback.DataCallback() {
//            @Override
//            public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
//                System.out.println("rc:" + rc);
//                System.out.println("path:" + path);
//                System.out.println("ctx:" + ctx);
//                System.out.println("data:" + new String(data));
//                System.out.println("stat:" + stat);
//            }
//        },"传给服务端的内容,会在异步回调时传回来");
//        Thread.sleep(2000);


//        System.out.println("*** 同步删除节点 *** ");
//        /**
//         * version =  -1 表示匹配任何版本
//         */
//        zookeeper1.delete("/node", -1);
//
//
//        System.out.println("*** 异步删除节点 *** ");
//        zookeeper1.delete("/node", -1, new AsyncCallback.VoidCallback() {
//            @Override
//            public void processResult(int rc, String path, Object ctx) {
//                System.out.println("rc:" + rc);
//                System.out.println("path:" + path);
//                System.out.println("ctx:" + ctx);
//            }
//        },"传给服务端的内容,会在异步回调时传回来");
//        Thread.sleep(2000);

//        System.out.println("*** 判断节点是否存在 *** ");
//        Stat exists = zookeeper1.exists("/node", false);
//        System.out.println(exists);
//        Stat stat = zookeeper1.exists("/node", new Watcher() {
//            public void process(WatchedEvent event) {
//                System.out.println("监视是否存在");
//            }
//        });
//        System.out.println(stat);


    }


    @Override
    public void process(WatchedEvent event) {
        System.out.println("Receive watched event:" + event);
        if (event.getState() == Event.KeeperState.SyncConnected) {
            System.out.println("zookeeper state is " + Event.KeeperState.SyncConnected);
        }
    }
}
